/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.demo.widget.meis.slice;

import com.demo.widget.ResourceTable;
import com.meis.widget.heart.HeartType;
import com.meis.widget.heart.MeiHeartView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.ImageInfo;
import ohos.media.image.common.Size;
import ohos.utils.PlainArray;

import java.io.IOException;

public class MeiHeartAbilitySlice extends AbilitySlice {
    private static final long ADD_HEART_DELAY_TIME = 1300L;
    private final int[] mHeartTypes = {
            HeartType.BLUE, HeartType.GREEN, HeartType.YELLOW,
            HeartType.PINK, HeartType.BROWN, HeartType.PURPLE, HeartType.RED};
    private final int[] mPixelMapResIds = {
            ResourceTable.Media_ic_heart_0, ResourceTable.Media_ic_heart_1, ResourceTable.Media_ic_heart_2,
            ResourceTable.Media_ic_heart_3, ResourceTable.Media_ic_heart_4, ResourceTable.Media_ic_heart_5,
            ResourceTable.Media_ic_heart_6};
    private MeiHeartView mHeartView;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_mei_heart);
        mHeartView = (MeiHeartView) findComponentById(ResourceTable.Id_heart);
        setHeartPixelMap();
        mHeartView.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mHeartView.addHeart();
            }
        });
        addHeart();
    }

    private void addHeart() {
        getUITaskDispatcher().delayDispatch(mAddHeartTask, ADD_HEART_DELAY_TIME);
    }

    private final Runnable mAddHeartTask = new Runnable() {
        @Override
        public void run() {
            mHeartView.addHeart();
            addHeart();
        }
    };

    public void setHeartPixelMap() {
        PlainArray<PixelMap> plainArray = new PlainArray<>();
        for (int i = 0; i < mHeartTypes.length; i++) {
            PixelMap pixelMap = createPixelMap(mPixelMapResIds[i]);
            if (pixelMap != null) {
                plainArray.put(mHeartTypes[i], pixelMap);
            }
        }
        mHeartView.setHeartBitmap(plainArray);
    }

    private PixelMap createPixelMap(int resId) {
        try {
            Resource resource = getResourceManager().getResource(resId);
            ImageSource imageSource = ImageSource.create(resource, new ImageSource.SourceOptions());
            ImageInfo imageInfo = imageSource.getImageInfo();
            final float scaleRatio = 1.5f;
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size((int) (imageInfo.size.width * scaleRatio), (int) (imageInfo.size.height * scaleRatio));
            return imageSource.createPixelmap(decodingOptions);
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mHeartView != null) {
            mHeartView.releaseAnimator();
        }
    }
}